package com.baizhi.config;

import com.baizhi.realm.AuthenRealm;
import lombok.Builder;
import org.apache.shiro.realm.AuthenticatingRealm;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.lang.reflect.MalformedParameterizedTypeException;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class shiroConfig   {
    /**
     * 过滤器工厂
     *
     * @Bean 所在的方法的方法形参 如果工厂中存在有相同类型的对象
     *  这个形参就会被自动装配
     */

    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(DefaultWebSecurityManager defaultWebSecurityManager){
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        Map map =new HashMap();
        //不拦截
        map.put("/login.jsp","anon");
        //拦截
        map.put("/index.jsp","authc");
        //拦截
        map.put("/guru/*","authc");
        //拦截
        map.put("/admin/*","authc");
        //拦截
        map.put("/banner/*","authc");


        shiroFilterFactoryBean.setFilterChainDefinitionMap(map);
        shiroFilterFactoryBean.setSecurityManager(defaultWebSecurityManager);
        return shiroFilterFactoryBean;

    }

    /**
     * @Bean 注解的方法形参中如果工厂中存在有型同类型的对象
     * 其方法形参中的值就会被自动装配
     *
     * @param authenRealm
     * @return
     */


    /**
     * 创建默认的安全管理器对象
     */
    @Bean
    public DefaultWebSecurityManager defaultWebSecurityManager(AuthenRealm authenRealm){
        DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
        /**
         * 设置自定义的realm
         */
        defaultWebSecurityManager.setRealm(authenRealm);

        return defaultWebSecurityManager;
    }


    /**
     * 自定义Realm
     */
    @Bean
    public AuthenRealm authenRealm(){
        return  new AuthenRealm();
    }

}
